package com.jing.cloud.service.chat.adapter.handler.impl;

import com.google.common.collect.Lists;
import com.jing.cloud.service.chat.adapter.MessageBean;
import com.jing.cloud.service.chat.adapter.helper.MessageHelper;
import com.jing.cloud.service.chat.bean.Message;
import com.jing.cloud.service.chat.bean.UserMessage;
import com.jing.cloud.service.chat.constant.ChatConstant;
import com.jing.cloud.service.chat.dao.MessageDao;
import com.jing.cloud.service.chat.dao.UserMessageDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by 29017 on 2017/9/10.
 */
@Component
@Slf4j
public class StorageMessageHandler extends AbstractMessageHandler {

    @Autowired
    private MessageDao messageDao;
    @Autowired
    private UserMessageDao userMessageDao;
    @Autowired
    private MessageHelper messageHelper;

    @Override
    public boolean handleMessage(MessageBean messageBean) {
        Message message = messageBean.convert2Message();
        messageDao.insert(message);
        List<Long> limitUser = messageHelper.getUserIds(messageBean);
        List<UserMessage> userMessages = Lists.transform(limitUser, (userId) -> {
            UserMessage userMessage = new UserMessage();
            userMessage.setUserId(userId);
            userMessage.setMessageId(message.getId());
            userMessage.setRoom(message.getRoom());
            userMessage.setScene(message.getScene());
            userMessage.setPush1(ChatConstant.MESSAGE_PUSH_NO);
            userMessage.setPush2(ChatConstant.MESSAGE_PUSH_NO);
            userMessage.setPush3(ChatConstant.MESSAGE_PUSH_NO);
            userMessage.setPush4(ChatConstant.MESSAGE_PUSH_NO);
            userMessage.setPush5(ChatConstant.MESSAGE_PUSH_NO);
            userMessage.setPush6(ChatConstant.MESSAGE_PUSH_NO);
            return userMessage;
        });
        userMessageDao.batchInsert(userMessages);
        return true;
    }

    @Override
    public int order() {
        return 100;
    }
}
